package com.demo.security.cloud.service.impl;

import com.demo.security.cloud.entity.TPermission;
import com.demo.security.cloud.entity.dto.PermissionDto;
import com.demo.security.cloud.mapper.TPermissionMapper;
import com.demo.security.cloud.service.ITPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.jsonwebtoken.lang.Collections;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.security.Permission;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liyan
 * @since 2020-06-19 20:16:20
 * @describe 服务实现类
 */
@Service("permissionService")
public class TPermissionServiceImpl extends ServiceImpl<TPermissionMapper, TPermission> implements ITPermissionService {

    @Override
    public List<PermissionDto> getPermissionsByUserId(Long userId) {
        List<TPermission> permissions = this.baseMapper.findByUserId(userId);
        if (Collections.isEmpty(permissions)){
            return null;
        }

        List<PermissionDto> dtos = new ArrayList<>(permissions.size());
        for (TPermission p : permissions){
            PermissionDto dto = new PermissionDto();
            BeanUtils.copyProperties(p, dto);
            dtos.add(dto);
        }

        return dtos;
    }
}
